package com.dk.controller;

import com.dk.pojo.playerData.PlayerData;
import com.dk.pojo.relaxation.Relaxation;
import com.dk.service.RelaxationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 摸鱼休闲
 */
@Controller
@RequestMapping("rel")
public class RelaxationController {
    @Autowired
    private RelaxationService relaxationService;

    //系统管理者看到的
    @RequestMapping("toShow")
    public String toShow(){
        return "/rel/show";
    }

    //玩家看到的（没有修改和删除功能）
    @RequestMapping("toPlayerShow")
    public String toPlayerShow(){
        return "/rel/playerShow";
    }

    //玩家私密网址
    @RequestMapping("toWorkShow")
    public String toWorkShow(){
        return "/rel/workShow";
    }

    //官方推荐的网址（说白了也就是秦无解推荐的网址）
    @RequestMapping("getInfo")
    @ResponseBody
    public Map<String,Object> getInfoServer(Relaxation relaxation,
                                            @RequestParam(defaultValue = "1")Integer pageNumber,
                                            @RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<Relaxation> page =  relaxationService.show(relaxation,pageNumber,pageSize);
        //需要total:总条数 rows:结果集 (都是key,value结合,所以用map)
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getList());
        return map;
    }

    //玩家私密网址
    @RequestMapping("getInfoByWork")
    @ResponseBody
    public Map<String,Object> getInfoByWork(HttpServletRequest request,
                                            Relaxation relaxation,
                                            @RequestParam(defaultValue = "1")Integer pageNumber,
                                            @RequestParam(defaultValue = "3")Integer pageSize){
        PlayerData playerData = (PlayerData) request.getSession().getAttribute("player");
        //获取该用户账号
        Integer playerAccount = playerData.getPlayerAccount();
        PageInfo<Relaxation> page =  relaxationService.showByWork(playerAccount,relaxation,pageNumber,pageSize);
        //需要total:总条数 rows:结果集 (都是key,value结合,所以用map)
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getList());
        return map;
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo(HttpServletRequest request, Relaxation relaxation){
        PlayerData playerData = (PlayerData) request.getSession().getAttribute("player");
        Integer playerAccount = playerData.getPlayerAccount();
        return relaxationService.addInfo(relaxation,playerAccount);
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        return "/rel/add";
    }

    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
        Relaxation relaxation = relaxationService.toUpdatet(id);
        model.addAttribute("relaxation",relaxation);
        return "/rel/update";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(HttpServletRequest request,Relaxation relaxation){
        PlayerData playerData = (PlayerData) request.getSession().getAttribute("player");
        Integer playerAccount = playerData.getPlayerAccount();
        return relaxationService.updateInfo(relaxation,playerAccount);
    }

    @RequestMapping("deleteInfo/{id}")
    @ResponseBody
    public boolean deleteInfo(@PathVariable("id")Integer id){
        return relaxationService.deleteInfo(id);
    }



}
